package ebj.common.crawler;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.omg.CORBA.WCharSeqHelper;

import ebj.common.utils.StringUtils;

public class JsoupTest {
	public static String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/6.0.5 Safari/536.30.1";
	
	public static void main(String[] args) {
		String file_path_js_location = "URL.Redirection-LazySwipe.302redirecr.result.html";
		String file_path_html_meta = "URL.Redirection-html.meta.html";
		
		File file_js_location = new File(file_path_js_location);
		File file_html_meta = new File(file_path_html_meta);
		
		System.out.println(file_js_location.getAbsolutePath());
		System.out.println(file_html_meta.getAbsolutePath());
		
		String html_code = "<!DOCTYPE html><html><head><script type=\"text/javascript\">function redirect(){window.location = \"http://trkyad.com/?a=2997&c=1243&s1=&s2=8132579206&s3=AP&s4=3101\"}</script></head><body onload=\"redirect();\"></body></html>";
		
		
		System.out.println("|||||||||||||||||||||||||||||||||");
		try {
			Document document = Jsoup.parse(file_js_location, "UTF-8");
			Document document2 = Jsoup.parse(html_code);
			// Elements elements = document.getAllElements();
			Elements elements = document2.getElementsByTag("script");
			for (Element element : elements) {
				String fun_str = element.html().replaceAll("\\s{2,}", "");
				System.out.println(fun_str);
				System.out.println(extractLocationFromJS(element.toString()));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("|||||||||||||||||||||||||||||||||");
		try {
			Document document = Jsoup.parse(file_html_meta, "UTF-8");
			Elements metas = document.select("html head meta");
			for (Element meta : metas) {
				String http_equiv = meta.attr("http-equiv");
				String url_redirection = meta.attr("content").split("=")[1];
				System.out.println(http_equiv);
				System.out.println(url_redirection);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	static String extractLocationFromJS(String script) {
        script = StringUtils.removeBlankCharacters(script);
        List<Pattern> patternList = new ArrayList<Pattern>();

        //location="http://example.com";
        //window.location = "http://trkyad.com/?a=2997&c=1243&s1=&s2=8132579206&s3=AP&s4=3101"
        patternList.add(Pattern.compile("location\\s{0,1}=\\s{0,1}\"(.*)\""));

        //document.location.href="http://example.com";
        patternList.add(Pattern.compile("location\\.href\\s{0,1}=\\s{0,1}\"(.*)\""));

        //window.location.reload("http://example.com");
        patternList.add(Pattern.compile("location\\.reload\\(\"(.*)\""));

        //document.location.replace("http://example.com");
        patternList.add(Pattern.compile("location\\.replace\\(\"(.*)\""));

        return extractLocationFromJS(script, patternList);
    }

    private static String extractLocationFromJS(String script, List<Pattern> patternList) {
        for (Pattern pattern : patternList) {
            Matcher matcher = pattern.matcher(script);
            if (matcher.find()) {
                return matcher.group(1);
            }
        }
        return null;
    }
}
